package com.example.administrator.myapplication.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.easemob.chat.EMMessage;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.view.CircleImageView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/29.
 */
public class ChatAdapter extends BaseAdapter {

    private Context mContext;
    private List<EMMessage> list = new ArrayList<EMMessage>();
    private int count;

    public ChatAdapter(Context con, List<EMMessage> list) {
        this.mContext = con;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_chat, parent, false);
            holder = new ViewHolder();
            holder.imLeft = (CircleImageView) convertView.findViewById(R.id.img_left);
            holder.imRight = (CircleImageView) convertView.findViewById(R.id.img_right);
            holder.txt_time = (TextView) convertView.findViewById(R.id.textView1);
            holder.txt_left = (TextView) convertView.findViewById(R.id.txt_left);
            holder.txt_right = (TextView) convertView.findViewById(R.id.txt_right);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Log.i("dddd", "---" + count);
        EMMessage message = list.get(position);
        Log.i("dddd", "---" + EMMessage.Direct.RECEIVE);
        if (message.direct == EMMessage.Direct.RECEIVE) {
            holder.imLeft.setVisibility(View.VISIBLE);
            holder.txt_left.setVisibility(View.VISIBLE);
            holder.imRight.setVisibility(View.GONE);
            holder.txt_right.setVisibility(View.GONE);

            String str = message.getBody().toString();
            String[] strs = str.split("\"");
            holder.txt_left.setText(strs[1]);
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String date = sdf.format(new java.util.Date());
            holder.txt_time.setText(date);
        } else {
            holder.imLeft.setVisibility(View.GONE);
            holder.txt_left.setVisibility(View.GONE);
            holder.imRight.setVisibility(View.VISIBLE);
            holder.txt_right.setVisibility(View.VISIBLE);
            String str = message.getBody().toString();
            String[] strs = str.split("\"");
            holder.txt_right.setText(strs[1]);
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String date = sdf.format(new java.util.Date());
            holder.txt_time.setText(date);
        }
        return convertView;
    }

    class ViewHolder {
        CircleImageView imLeft, imRight;
        TextView txt_time, txt_left, txt_right, txt_time2;
    }

}
